package com.person.monitor;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.google.gson.Gson;

public class QuickShPref {

	private Context mContext;
	private boolean isMainProcess;
	private String mPrefName;

	private static QuickShPref mInstance;

	public static QuickShPref getInstance(){
		return mInstance;
	}

	public static void init(Context context){
		mInstance = new QuickShPref(context);
	}

	public QuickShPref(Context context){
		mContext = context;
		isMainProcess = true;
	}

	synchronized public void putValueObject(String key , Object obj){
		synchronized (mContext) {
			Editor sEditor = getPref().edit();
			if (obj instanceof String) {
				sEditor.putString(key, (String) obj);
			} else if (obj instanceof Integer) {
				sEditor.putInt(key, (Integer) obj);
			} else if (obj instanceof Long) {
				sEditor.putLong(key, (Long) obj);
			} else if (obj instanceof Boolean) {
				sEditor.putBoolean(key, (Boolean) obj);
			} else if (obj instanceof Float) {
				sEditor.putFloat(key, (Float) obj);
			} else {
				return;
			}
			sEditor.commit();
		}
	}
	
	public String getString(String key){
		String ret;
		synchronized (mContext) {
			ret = getPref().getString(key, "");
		}
		return ret;
	}
	public int getInt(String key){
		int ret;
		synchronized (mContext) {
			 ret = getPref().getInt(key, -1);
		}
		return ret;
	}
	public boolean getBoolean(String key){
		boolean ret;
		synchronized (mContext) {
			 ret = getPref().getBoolean(key, false);
		}
		MyLog.D(key+"="+ret);
		return ret;
	}
	public boolean getBoolean(String key,boolean def){
		boolean ret;
		synchronized (mContext) {
			ret = getPref().getBoolean(key, def);
		}
		MyLog.D(key+"="+ret);
		return ret;
	}
	public Float getFloat(String key){
		Float ret;
		synchronized (mContext) {
			 ret = getPref().getFloat(key, 0f);
		}
		return ret;
	}
	public Long getLong(String key){
		Long ret;
		synchronized (mContext) {
			ret = getPref().getLong(key, 0L);
		}
		return ret;
	}

	public <T> T getClass(String key, Class<T> classOfT){
		String json = getString(key);
		T obj = null;
		if(TextUtils.isEmpty(json) == false) {
			obj = new Gson().fromJson(json, classOfT);
		}
		return obj;
	}

	public <T> void putGsonClass(String key, T t){
		if(t == null){
			putValueObject(key, "");
		}else {
			String json = new Gson().toJson(t);
			putValueObject(key, json);
		}
	}

	public void removeKey(String key){
		synchronized (mContext) {
			Editor sEditor = getPref().edit();
			sEditor.remove(key);
			sEditor.commit();
		}
	}

	public void removeAll(){
		synchronized (mContext) {
			Editor sEditor = getPref().edit();
			sEditor.clear();
			sEditor.commit();
		}
	}

	public SharedPreferences getPref(){
		return mContext.getSharedPreferences(getPrefName(), Context.MODE_PRIVATE);
	}

	private String getPrefName(){
		if(TextUtils.isEmpty(mPrefName)){
			mPrefName = mContext.getPackageName() + "_" + isMainProcess;
		}
		return mPrefName;
	}
}
